package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.entity.SysCategory;
import com.blog.entity.SysAssociate;
import com.blog.entity.SysUser;
import com.blog.service.CategoryService;
import com.blog.service.AssociateService;
import com.blog.util.StringUtil;

import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import com.blog.util.uuid.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author cgx
 * @Date 2021/10/9 15:34
 * 博客分类控制器
 */
@Controller
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @Resource
    AssociateService associateService;
    /**
     * 首页左侧展示博客分类
     * @param
     * @return
     */
    @PostMapping("queryAllCategory")
    @ResponseBody
    public AjaxResult queryAllArticleCategory(){
        QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled",1);
        queryWrapper.orderByAsc("create_time");
        List<SysCategory> list = categoryService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 后台查询所有博客（包括状态）
     * @param
     * @return
     */
    @GetMapping("/admin/tableList")
    @ResponseBody
    public LayDto tableList(@RequestParam(value = "createBy",required = false) String createBy,
                            @RequestParam(value = "categoryName",required = false) String categoryName){
        QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("create_time");
        if (StringUtil.isNotBlank(createBy)){
            queryWrapper.like("create_by",createBy);
        }
        if (StringUtil.isNotBlank(categoryName)){
            queryWrapper.like("category_name",categoryName);
        }

        List<SysCategory> list = categoryService.list(queryWrapper);
        return LayDtoUtil.returnSuccess(0,"",list.size(),list);

    }

    /**
     * 后台添加分类跳转页面
     */
    @GetMapping("admin/add")
    public String addCategory(){
        return "admin/article/category/category-add";
    }

    /**
     * 后台修改页面跳转
     */
    @GetMapping("admin/edit")
    public String toEditCategory(Model model,@RequestParam("categoryId") String categoryId){
        SysCategory category = categoryService.getById(categoryId);
        model.addAttribute("category",category);
        return "admin/article/category/category-edit";
    }

    /**
     * 后台编辑分类
     * @param
     * @return
     */
    @PostMapping("admin/updateCategory")
    @ResponseBody
    public AjaxResult updateCategory(@RequestBody SysCategory category){
        categoryService.updateById(category);
        return AjaxResult.success();
    }

    /**
     * 后台添加分类保存
     * @param
     * @return
     */
    @PostMapping("admin/saveCategory")
    @ResponseBody
    public AjaxResult saveCategory(@RequestBody SysCategory category){

        QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name",category.getCategoryName());
        List<SysCategory> list = categoryService.list(queryWrapper);
        if (list.size()>0){
            return AjaxResult.error("分类已存在");
        }
        //获取登录用户数据
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser)subject.getPrincipal();
        category.setId(UUIDUtils.getUUId());
        category.setType(1);
        category.setActive(1);
        category.setCreateBy(sysUser.getUsername());

        categoryService.save(category);
        return AjaxResult.success();
    }

    /**
     * 改变分类状态
     * @param
     * @return
     */
    @PostMapping("admin/changeCategoryActive")
    @ResponseBody
    public AjaxResult changeCategoryActive(@RequestParam("categoryId") String categoryId,@RequestParam("sign") String sign) {
        //启用
        if ("ON".equals(sign)) {
            categoryService.updateActive(1, categoryId);
            return AjaxResult.success();
        }
        //停用
        if ("OFF".equals(sign)) {
            QueryWrapper<SysAssociate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", categoryId);
            List<SysAssociate> list = associateService.list(queryWrapper);
            if (list.size() > 0) {
                return AjaxResult.error("该分类下存在数据");
            }
            categoryService.updateActive(-1, categoryId);
            return AjaxResult.success();
        }
        return AjaxResult.error("处理失败");
    }

    /**
     * 后台删除
     * @param
     * @return
     */
    @PostMapping("admin/deleteById")
    @ResponseBody
    public AjaxResult articleDeleteById(@RequestParam("categoryId") String categoryId){
        QueryWrapper<SysAssociate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<SysAssociate> list = associateService.list(queryWrapper);
        if (list.size()>0){
            return AjaxResult.error("该分类下存在数据");
        }
        categoryService.removeById(categoryId);
        return AjaxResult.success();
    }
}
